﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using jiaju.Areas.Back.Models.DBModel;
using System.Data.SqlClient;

namespace jiaju.Areas.Back.Models.DAL
{
    public class UserService
    {
        public static User GetAlluser()
        {
            string sql = "select * from Users";
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            User user = null;
            while (sdr.Read())
            {
                int Id = (int)sdr["Id"];
                string UserName = (string)sdr["UserName"];
                string PassWord = (string)sdr["PassWord"];
                string ImgUrl = (string)sdr["ImgUrl"];
                string Cont = (string)sdr["Cont"];
                user = new User()
                {
                    Id = Id,
                    UserName = UserName,
                    PassWord = PassWord,
                    ImgUrl = ImgUrl,
                    Cont = Cont
                };
            }
            return user;
        }
        public static User getUser(string UserName)
        {
            string sql = string.Format("select UserName,PassWord,ImgUrl,Cont from dbo.Users where UserName='{0}'", UserName);
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            User use = null;
            while (sdr.Read())
            {
                string UserNam = (string)sdr["UserName"];
                string PassWord = (string)sdr["PassWord"];
                string ImgUrl = (string)sdr["ImgUrl"];
                string Cont = (string)sdr["Cont"];
                use = new User()
                {
                    UserName = UserNam,
                    PassWord = PassWord,
                    ImgUrl = ImgUrl,
                    Cont=Cont
                };
            }
            return use;
        }
        //密码
        public static User getpwd(string password)
        {
            string sql = string.Format("select UserName,PassWord,ImgUrl,Cont from dbo.Users where Password='{0}'", password);
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            User user1 = null;
            while (sdr.Read())
            {
                string UserName = (string)sdr["UserName"];
                string PassWord = (string)sdr["PassWord"];
                string ImgUrl = (string)sdr["ImgUrl"];
                string Cont = (string)sdr["Cont"];
                user1 = new User()
                {
                    UserName = UserName,
                    PassWord = PassWord,
                    ImgUrl = ImgUrl,
                     Cont = Cont
                };
            }
            return user1;
        }
        //编辑，请求数据库
        public static int EditArticle(User user)
        {
            String sql = string.Format(@"UPDATE dbo.Users
   SET ImgUrl ='{0}'  WHERE UserName='{1}'", user.ImgUrl, user.UserName);
            int num = DBHelp.ExecuteNonQuery(sql);
            return num;
        }
        public static User GetById()//获取用户表数据
        {
            string sql = "select * from dbo.Users";
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            User name = null;
            while (sdr.Read())
            {
                string userName = (string)sdr["UserName"];
                string PassWord = (string)sdr["PassWord"];
                string ImgUrl = (string)sdr["ImgUrl"];
                string Cont = (string)sdr["Cont"];
                name = new User()
                {
                    UserName = userName,
                    PassWord = PassWord,
                    ImgUrl = ImgUrl,
                    Cont = Cont
                };
            }
            return name;
        }
        public static int Editpwd(string UserName, string NewPassword)//修改密码
        {
            string sql = string.Format(@"UPDATE dbo.Users SET 
                                     Password ='{0}' WHERE UserName='{1}'",
                                      NewPassword, UserName);
            int num = DBHelp.ExecuteNonQuery(sql);
            return num;
        }
    }
}